/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { ScreenBreakpoint } from './Breakpoint'
import { HarmonyOrientation } from './HarmonyOrientation';

export enum VideoDirection {
  LANDSCAPE,
  PORTRAIT
}

export enum ScreenStatus {
  PORTRAIT_FULLSCREEN,
  LANDSCAPE_FULLSCREEN,
  NON_FULLSCREEN
}

export interface AdaptiveRotationRule {
  screenBreakpointRanges: ScreenBreakpoint[],
  videoDirection: VideoDirection,
  screenStatus: ScreenStatus,
  orientations: number[];
}

export function getCustomRotationInfo(screenBreakpoint: ScreenBreakpoint, videoDirection: VideoDirection, screenStatus: ScreenStatus): number[] {
  let defaultIndex = 0;
  defaultRotationRules.forEach((item, index) => {
    if (item.screenBreakpointRanges.find(item => item === screenBreakpoint) &&
      videoDirection === item.videoDirection && screenStatus === item.screenStatus) {
      defaultIndex = index;
    }
  });

  return defaultRotationRules[defaultIndex].orientations;
}

const defaultRotationRules: AdaptiveRotationRule[] = [
  {
    /**
     * 规则 1:
     * 断点区间：Small_Portrait, Medium_Landscape (如：直板机)
     * 屏幕状态：横向全屏
     * 视频方向：横向视频
     * 旋转规则：
     * USER_ROTATION_LANDSCAPE：调用时临时旋转到横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且可旋转方向受系统判定
     * (如在某种设备，可以旋转到竖屏、横屏、反向横屏三个方向，无法旋转到反向竖屏)
     * USER_ROTATION_LANDSCAPE_INVERTED：调用时临时旋转到反向横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且可旋转方向受系统判定
     * (如在某种设备，可以旋转到竖屏、横屏、反向横屏三个方向，无法旋转到反向竖屏)
     * */
    screenBreakpointRanges: [ScreenBreakpoint.SMALL_PORTRAI, ScreenBreakpoint.MEDIUM_LANDSCAPE],
    screenStatus: ScreenStatus.LANDSCAPE_FULLSCREEN,
    videoDirection: VideoDirection.LANDSCAPE,
    orientations: [
      HarmonyOrientation.USER_ROTATION_LANDSCAPE,
      HarmonyOrientation.USER_ROTATION_LANDSCAPE_INVERTED,
      HarmonyOrientation.USER_ROTATION_LANDSCAPE,
      HarmonyOrientation.USER_ROTATION_LANDSCAPE
    ]
  },
  /**
   * 规则 2:
   * 断点区间：Small_Portrait, Medium_Landscape, Small_Square (如：直板机、purax折叠形态)
   * 屏幕状态：竖向全屏
   * 视频方向：竖向视频
   * 旋转规则：
   * PORTRAIT：调用后竖屏显示
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.SMALL_PORTRAI, ScreenBreakpoint.MEDIUM_LANDSCAPE, ScreenBreakpoint.SMALL_SQUARE],
    screenStatus: ScreenStatus.PORTRAIT_FULLSCREEN,
    videoDirection: VideoDirection.PORTRAIT,
    orientations: [
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT
    ]
  },
  /**
   * 规则 3:
   * 断点区间：Small_Portrait, Medium_Landscape (如：直板机)
   * 屏幕状态：非全屏
   * 视频方向：横向视频
   * 旋转规则：
   * USER_ROTATION_PORTRAIT：调用时临时旋转到竖屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且可旋转方向受系统判定
   * (如在某种设备，可以旋转到竖屏、横屏、反向横屏三个方向，无法旋转到反向竖屏)
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.SMALL_PORTRAI, ScreenBreakpoint.MEDIUM_LANDSCAPE],
    screenStatus: ScreenStatus.NON_FULLSCREEN,
    videoDirection: VideoDirection.LANDSCAPE,
    orientations: [
      HarmonyOrientation.USER_ROTATION_PORTRAIT,
      HarmonyOrientation.USER_ROTATION_PORTRAIT,
      HarmonyOrientation.USER_ROTATION_PORTRAIT,
      HarmonyOrientation.USER_ROTATION_PORTRAIT
    ]
  },
  /**
   * 规则 4:
   * 断点区间：Small_Portrait, Medium_Landscape, Small_Square (如：直板机、purax折叠形态)
   * 屏幕状态：非全屏
   * 视频方向：竖向视频
   * 旋转规则：
   * PORTRAIT：调用后竖屏显示
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.SMALL_PORTRAI, ScreenBreakpoint.MEDIUM_LANDSCAPE, ScreenBreakpoint.SMALL_SQUARE],
    screenStatus: ScreenStatus.NON_FULLSCREEN,
    videoDirection: VideoDirection.PORTRAIT,
    orientations: [
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT
    ]
  },
  /**
   * 规则 5:
   * 断点区间：Small_Square (如：purax折叠形态)
   * 屏幕状态：非全屏
   * 视频方向：竖向视频
   * 旋转规则：
   * PORTRAIT：调用后竖屏显示
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.SMALL_SQUARE],
    screenStatus: ScreenStatus.NON_FULLSCREEN,
    videoDirection: VideoDirection.LANDSCAPE,
    orientations: [
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT
    ]
  },
  /**
   * 规则 6:
   * 断点区间：Small_Square (如：purax折叠形态)
   * 屏幕状态：横向全屏
   * 视频方向：横向视频
   * 旋转规则：
   * PORTRAIT：调用后竖屏显示
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.SMALL_SQUARE],
    screenStatus: ScreenStatus.LANDSCAPE_FULLSCREEN,
    videoDirection: VideoDirection.LANDSCAPE,
    orientations: [
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT,
      HarmonyOrientation.PORTRAIT
    ]
  },
  /**
   * 规则 7:
   * 断点区间：Medium_Portrait, Medium_Square, Large_Portrait, Large_Square, Large_Landscape, XLarge_Landscape (如：平板、2in1、x5、xt展开形态)
   * 屏幕状态：横向全屏
   * 视频方向：横向视频
   * 旋转规则：
   * AUTO_ROTATION_UNSPECIFIED：调用时临时旋转到横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且可旋转方向受系统判定
   * (如在某种设备，可以旋转到竖屏、横屏、反向横屏三个方向，无法旋转到反向竖屏)
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.MEDIUM_PORTRAI, ScreenBreakpoint.MEDIUM_SQUARE, ScreenBreakpoint.LARGE_PORTRAI, ScreenBreakpoint.LARGE_SQUARE, ScreenBreakpoint.LARGE_LANDSCAPE, ScreenBreakpoint.XLARGE_LANDSCAPE],
    screenStatus: ScreenStatus.LANDSCAPE_FULLSCREEN,
    videoDirection: VideoDirection.LANDSCAPE,
    orientations: [
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED
    ]
  },
  /**
   * 规则 8:
   * 断点区间：Medium_Portrait, Medium_Square, Large_Portrait, Large_Square, Large_Landscape, XLarge_Landscape (如：平板、2in1、x5、xt展开形态)
   * 屏幕状态：竖向全屏
   * 视频方向：竖向视频
   * 旋转规则：
   * AUTO_ROTATION_UNSPECIFIED：调用时临时旋转到横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且可旋转方向受系统判定
   * (如在某种设备，可以旋转到竖屏、横屏、反向横屏三个方向，无法旋转到反向竖屏)
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.MEDIUM_PORTRAI, ScreenBreakpoint.MEDIUM_SQUARE, ScreenBreakpoint.LARGE_PORTRAI, ScreenBreakpoint.LARGE_SQUARE, ScreenBreakpoint.LARGE_LANDSCAPE, ScreenBreakpoint.XLARGE_LANDSCAPE],
    screenStatus: ScreenStatus.PORTRAIT_FULLSCREEN,
    videoDirection: VideoDirection.PORTRAIT,
    orientations: [
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED
    ]
  },
  /**
   * 规则 9:
   * 断点区间：Medium_Portrait, Medium_Square, Large_Portrait, Large_Square, Large_Landscape, XLarge_Landscape (如：平板、2in1、x5、xt展开形态)
   * 屏幕状态：非全屏
   * 视频方向：横向视频
   * 旋转规则：
   * AUTO_ROTATION_UNSPECIFIED：调用时临时旋转到横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且可旋转方向受系统判定
   * (如在某种设备，可以旋转到竖屏、横屏、反向横屏三个方向，无法旋转到反向竖屏)
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.MEDIUM_PORTRAI, ScreenBreakpoint.MEDIUM_SQUARE, ScreenBreakpoint.LARGE_PORTRAI, ScreenBreakpoint.LARGE_SQUARE, ScreenBreakpoint.LARGE_LANDSCAPE, ScreenBreakpoint.XLARGE_LANDSCAPE],
    screenStatus: ScreenStatus.NON_FULLSCREEN,
    videoDirection: VideoDirection.LANDSCAPE,
    orientations: [
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED
    ]
  },
  /**
   * 规则 10:
   * 断点区间：Medium_Portrait, Medium_Square, Large_Portrait, Large_Square, Large_Landscape, XLarge_Landscape (如：平板、2in1、x5、xt展开形态)
   * 屏幕状态：非全屏
   * 视频方向：竖向视频
   * 旋转规则：
   * AUTO_ROTATION_UNSPECIFIED：调用时临时旋转到横屏，之后跟随传感器自动旋转，受控制中心的旋转开关控制，且可旋转方向受系统判定
   * (如在某种设备，可以旋转到竖屏、横屏、反向横屏三个方向，无法旋转到反向竖屏)
   * */
  {
    screenBreakpointRanges: [ScreenBreakpoint.MEDIUM_PORTRAI, ScreenBreakpoint.MEDIUM_SQUARE, ScreenBreakpoint.LARGE_PORTRAI, ScreenBreakpoint.LARGE_SQUARE, ScreenBreakpoint.LARGE_LANDSCAPE, ScreenBreakpoint.XLARGE_LANDSCAPE],
    screenStatus: ScreenStatus.NON_FULLSCREEN,
    videoDirection: VideoDirection.PORTRAIT,
    orientations: [
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED,
      HarmonyOrientation.AUTO_ROTATION_UNSPECIFIED
    ]
  }
]
